local skel = fk.CreateSkill {
  name = "emo__nixi",
}

Fk:loadTranslationTable{
  ["emo__nixi"] = "塔萨法隆格逆袭",
  [":emo__nixi"] = "出牌阶段，你可用所有手牌与牌堆顶的四张牌交换并视为使用一张【兵临城下】。若没有造成伤害，此技能无效至本阶段结束。",

  ["#emo__nixi"] = "逆袭：用所有手牌与牌堆顶的四张牌交换，视为使用一张【兵临城下】",
  ["#emo__nixi-guanxing"] = "逆袭：你的手牌将被放置于牌堆顶，你可调整顺序",
  ["$EmoPrivatePut"] = "%from 的 %card 被至于牌堆顶",

  ["$emo__nixi1"] = "斗争……从未改变",
  ["$emo__nixi2"] = "放轻松，痛苦只有一瞬间",
}

skel:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  prompt = "#emo__nixi",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = player:getCardIds("h")
    local top = room:getNCards(4)

    -- 先把两堆牌移动到处理区再说
    room:moveCards({
      from = player,
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = self.name,
      moveVisible = false,
      visiblePlayers = player,
    },{
      ids = top,
      toArea = Card.Processing,
      moveReason = fk.ReasonExchange,
      proposer = player,
      skillName = self.name,
      moveVisible = false,
      --visiblePlayers = player,
    })

    cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
    top = table.filter(top, function (id) return room:getCardArea(id) == Card.Processing end)
    if not player.dead and #cards > 1 then
      local guanxing = room:askToGuanxing(player, {
        cards = cards, bottom_limit = {0, 0}, skill_name = skel.name, title = skel.name, skip = true, prompt = "#emo__nixi-guanxing",
      })
      -- 进入牌堆是倒序进入的，故需要反转一下……
      cards = table.reverse(guanxing.top)
    end

    local moveInfos = {}
    if #cards > 0 then
      table.insert(moveInfos, {
        ids = cards,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonExchange,
        proposer = player,
        skillName = self.name,
        moveVisible = false,
        visiblePlayers = player,
      })
      -- 仅给单人发动消息，未封装，有点危险
      local log = { type = "$EmoPrivatePut", from = player.id, card = table.reverse(cards) }
      player:doNotify("GameLog", json.encode(log))
    end

    if not player.dead then
      table.insert(moveInfos, {
        ids = top,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonExchange,
        proposer = player,
        skillName = self.name,
        moveVisible = false,
        visiblePlayers = player,
      })
    end
    if #moveInfos > 0 then
      room:moveCards(table.unpack(moveInfos))
    end
    room:cleanProcessingArea(table.connect(cards, top), self.name)

    if player.dead then return end
    local use = room:askToUseVirtualCard(player, {
      name = "emo__enemy_at_the_gates", cancelable = false, skill_name = skel.name, skip = true,
    })
    -- 兵临城下亮出的未使用牌将塞回牌堆（手杀版）
    if use then
      use.extra_data = use.extra_data or {}
      use.extra_data.toArea = "top"
      room:useCard(use)
    end
    if not (use and use.extra_data and use.extra_data.enemy_at_the_gates_damageInfo) then
      room:invalidateSkill(player, self.name, "-phase")
    end
  end,
})

return skel
